﻿// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// This file is part of CodingEditor.
// Note:	This project is derived from Peter Project
//			(hosted on sourceforge and codeplex)
//
// Copyright (c) 2008-2009, CE Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


namespace CE.Classes
{
	using System;
	using System.Drawing;
	using System.Runtime.InteropServices;
	using System.Windows.Forms;

	public class FormState
	{
		#region Fields

		public bool IsMaximized;

		private Rectangle bounds;
		private FormBorderStyle brdStyle;
		private bool topMost;
		private FormWindowState winState;

		#endregion Fields

		#region Methods

		public bool Maximize(Form targetForm)
		{
			if (IsMaximized) return true;
			IsMaximized = true;
			Save(targetForm);
			targetForm.WindowState = FormWindowState.Maximized;
			targetForm.FormBorderStyle = FormBorderStyle.None;
			targetForm.TopMost = true;
			WinAPI.SetWinFullScreen(targetForm.Handle);

			return IsMaximized;
		}

		public bool Restore(Form targetForm)
		{
			targetForm.WindowState = winState;
			targetForm.FormBorderStyle = brdStyle;
			targetForm.TopMost = topMost;
			targetForm.Bounds = bounds;
			IsMaximized = false;
			return IsMaximized;
		}

		public void Save(Form targetForm)
		{
			winState = targetForm.WindowState;
			brdStyle = targetForm.FormBorderStyle;
			topMost = targetForm.TopMost;
			bounds = targetForm.Bounds;
		}

		#endregion Methods
	}

	// Source: http://www.codeproject.com/KB/cs/FullScreenDotNetApp.aspx
	public class WinAPI
	{
		#region Fields

		private static readonly IntPtr HWND_TOP = IntPtr.Zero;

		private const int SM_CXSCREEN = 0;
		private const int SM_CYSCREEN = 1;
		private const int SWP_SHOWWINDOW = 64; // 0×0040

		#endregion Fields

		#region Properties

		public static int ScreenX
		{
			get { return GetSystemMetrics(SM_CXSCREEN); }
		}

		public static int ScreenY
		{
			get { return GetSystemMetrics(SM_CYSCREEN); }
		}

		#endregion Properties

		#region Methods

		[DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
		public static extern int GetSystemMetrics(int which);

		public static void SetWinFullScreen(IntPtr hwnd)
		{
			SetWindowPos(hwnd, HWND_TOP, 0, 0, ScreenX, ScreenY, SWP_SHOWWINDOW);
		}

		[DllImport("user32.dll")]
		public static extern void SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
		                                       int X, int Y, int width, int height, uint flags);

		#endregion Methods
	}
}